//
//  TORadioViewController.m
//  B段项目
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TORadioViewController.h"
#import "TORadioTableCell.h"
#import "HLJHomeHeaderView.h"
#import "TORadioHeader.h"
#import "TORadioDetailViewController.h"
#import "TOAnimatedView.h"
#import "TOTools.h"

@interface TORadioViewController ()<UITableViewDataSource, UITableViewDelegate,TORadioHeaderDelegate>

@property(nonatomic,strong)UITableView *radioTableView;
@property(nonatomic,strong)NSMutableArray *hotArray;
@property(nonatomic,strong)NSMutableArray *allListArray;
@property(nonatomic,strong)NSMutableArray *lunArray;
@property(nonatomic,strong)TORadioHeader *radioHeader;
@property(nonatomic,assign)NSInteger nextPage;

@end

@implementation TORadioViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.titleLabel.text = @"电台";

    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-erji3.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    if ([ZZCMusicPlayerViewController shareMusicPlayerViewController].allRadioSelected == YES) {
        
        self.leftImage.image = [UIImage imageNamed:@"iconfont-houtui.png"];
    }
 
    NSLog(@"8888888  %d", [ZZCMusicPlayerViewController shareMusicPlayerViewController].allRadioSelected);
}

-(void)leftAction:(UIButton *)leftAction
{
    
    if ([ZZCMusicPlayerViewController shareMusicPlayerViewController].allRadioSelected == YES) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [super leftAction:leftAction];
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    if ([ZZCMusicPlayerViewController shareMusicPlayerViewController].allRadioSelected == YES) {

        [self.leftButton removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.rightView removeFromSuperview];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
    
   

    
}





#pragma mark - 创建radioTableView

-(void)createTableView
{
    self.radioTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    [self.view addSubview:self.radioTableView];
    
    [self.radioTableView registerClass:[TORadioTableCell class] forCellReuseIdentifier:@"TORadioTableCell"];
    
#pragma mark - 创建头视图
    
    self.radioHeader = [[TORadioHeader alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2)];
    self.radioTableView.tableHeaderView = self.radioHeader;
    self.radioHeader.radioHeaderDelegate = self;
    
#pragma mark - 下拉刷新
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getAFNetWorkingDataIsUpLoad)];
    self.radioTableView.mj_footer = footer;
    self.nextPage = 9;
    
#pragma mark - 上拉刷新
    MJRefreshGifHeader *header = [TOTools createTableViewGifHeaderWithTarget:self Action:@selector(getData)];
    self.radioTableView.mj_header = header;
    [self.radioTableView.mj_header beginRefreshing];
    
}

#pragma mark - 点击小图跳转到详情页面
-(void)jumpToNextPage:(NSInteger)Imagetag
{
    TORadioModel *model = _hotArray[Imagetag];
    TORadioDetailViewController *radioDetail = [[TORadioDetailViewController alloc] init];
    radioDetail.radioId = model.radioid;
    radioDetail.titleStr = model.title;
    [self.navigationController pushViewController:radioDetail animated:YES];
    
}

#pragma mark - 点击轮播图跳转
-(void)presentToDetailVCByRadioID:(NSString *)radioId
{
    TORadioDetailViewController *radioDetail = [[TORadioDetailViewController alloc] init];
    radioDetail.radioId = radioId;
    [self.navigationController pushViewController:radioDetail animated:YES];
}

#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allListArray.count;
}

#pragma mark 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TORadioTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TORadioTableCell"];
    
    TORadioModel * model = self.allListArray[indexPath.row];
    
    cell.radioMl = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT/6;
}

#pragma mark - 数据请求
-(void)getData
{
    self.hotArray = [NSMutableArray array];
    self.allListArray = [NSMutableArray array];
    NSString *urlStr = @"http://api2.pianke.me/ting/radio";
    NSString *body = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *hotArray = [bigDic objectForKey:@"hotlist"];
        NSMutableArray *allListArray = [bigDic objectForKey:@"alllist"];
        self.lunArray = [bigDic objectForKey:@"carousel"];//轮播图数组
//        NSLog(@"%@", self.lunArray);
        for (NSMutableDictionary *hotDic in hotArray) {
            TORadioModel *radMl = [[TORadioModel alloc]init];
//            NSLog(@"%@", hotDic);
            [radMl setValuesForKeysWithDictionary:hotDic];
            //三个小块区数组
            [self.hotArray addObject:radMl];
        }
        
        for (NSMutableDictionary *allDic in allListArray) {
            TORadioModel *radio = [[TORadioModel alloc]init];
            [radio setValuesForKeysWithDictionary:allDic];
            //全部radio
            [self.allListArray addObject:radio];
        }
        
        [self.radioHeader setHeaderArray:self.hotArray ShowArray:self.lunArray];
        
        [self.radioTableView reloadData];
        [self.radioTableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark- 上拉解析数据
- (void)getAFNetWorkingDataIsUpLoad{
    
    
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_list";
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=9&start=%ld&version=3.0.6", self.nextPage];
    
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
//        NSLog(@"%@", result);
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            TORadioModel *radioMl = [[TORadioModel alloc] init];
            [radioMl setValuesForKeysWithDictionary:dic];
            [self.allListArray addObject:radioMl];
        }
        
        self.nextPage += 9;
        [self.radioTableView.mj_footer endRefreshing];
        [self.radioTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}

#pragma mark - tableView点击跳转到详情页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TORadioDetailViewController *detailVC = [[TORadioDetailViewController alloc] init];
    
    
    TORadioModel *allModel = [self.allListArray objectAtIndex:indexPath.row];
    
    detailVC.titleStr = allModel.title;
    detailVC.radioId = allModel.radioid;
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%ld", self.radioTableView.mj_header.state);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
